import { useState, useCallback, useRef } from 'react';

import defaultAvatar from '@/assets/pc/images/default-avatar.png';
import { allGet } from '@/services/pc';
import storage from '@/utils/storage';
import { TOKEN, USERINFO } from '@/common/const';

export default () => {
  const [userInfo, setUseInfoData] = useState<{
    avatar: string;
    isadmin?: any;
    isteacher?: number;
    needcontact?: number;
    newpm?: string;
    uid?: string;
    username?: string;
    verify?: number;
    extcredits1?: any;
    extcredits2?: any;
    newsystem?: any;
    newprompt?: any;
    teacher?: any;
    isstudent?: boolean;
  }>({
    avatar: '',
  });
  const isInit = useRef(false);

  // 获取用户数据
  const getUseInfo = async () => {
    const token = storage.get(TOKEN);
    if (!token?.value) return;
    const response = await allGet({ params: { mod: 'user', do: 'member', version: 'pc1.0' } });
    const data = {
      ...(response?.data ?? {}),
    };
    isInit.current = true;
    storage.set(USERINFO, data);
    setUseInfoData(data);
  };

  const setUseInfo = (data: Record<string, any>) => {
    setUseInfoData({
      ...userInfo,
      ...data,
    });
  };

  return {
    isInit,
    userInfo,
    getUseInfo,
    setUseInfo,
  };
};
